/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package base;

import java.util.List;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.chain.ChainNode.MSG;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.variable.types.ArrayType;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty.Flag;
import tal.drivechain.variable.VarTableAction;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Tal Eisenberg
 */
public class Iterate extends Module {
    VarMatcher trigger;
    Variable output;

    Variable idx, element;
    VarTableAction setIdx, setElement;

    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        idx = new Variable("idx", DataType.INT, -1, true);
        setIdx = new VarTableAction(idx.getName(), VarTableAction.Action.SET_ANY);
        element = new Variable("element", DataType.EVENT, null, true);
        setElement = new VarTableAction(element.getName(), VarTableAction.Action.SET_ANY);

        this.setTrigger(props.getProperty("trigger"), null);
        this.setOutput(props.getProperty("output"), null);
    }

    public void setTrigger(Object val, Flag flag) {
        trigger = (VarMatcher)val;
    } public VarMatcher getTrigger() { return trigger; }

    public void setOutput(Object val, Flag flag) {
        output = (Variable)val;
    } public Variable getOutput() { return output; }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (trigger.matches(in, channel, this)) {
            if (!(in.getDataType() instanceof ArrayType)) {
                this.log("Received a matching non-array message.", MSG.WARNING);
                return;
            } else {
                DataType elementType = ((ArrayType)in.getDataType()).getElementType();
                element.setDataType(elementType);
                List array = (List)in.getValue(this);
                
                for (int i=0; i<array.size(); i++) {
                    Object val = array.get(i);
                    element.setValue(val, true);
                    setElement.set(element, localVars, this);

                    idx.setValue(i, true);
                    setIdx.set(idx, localVars, this);

                    sendToOutputs(output, timestamp);
                }
            }
        }
    }

}
